﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace FileBrowse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();
            tn.ImageIndex = 0;   //设置结点图片样式
            GetList(@"c:\", tn, 2);
            fileTree.Nodes.Add(tn);
            fileTree.Nodes[0].Text = "C:";   
            fileTree.Nodes[0].Expand();   //默认展开一级子结点
        }

        //展开子结点时，调用GetList设置下一级子结点
        private void fileTree_AfterExpand(object sender, TreeViewEventArgs e)
        {
            GetList(e.Node.Name, e.Node, 2);
        }

        //获取路径下目录并显示到树控件
        private void GetList(string path, TreeNode tn, int times)
        {
            if (times < 0)
                return;
            try
            {
                DirectoryInfo files = new DirectoryInfo(path);
                DirectoryInfo[] dinfo = files.GetDirectories();
                for(int m = 0;m < dinfo.Length;m++)
                {
                    if(tn.Name != path)
                    {
                        tn.Nodes.Add(dinfo[m].FullName, Path.GetFileNameWithoutExtension(dinfo[m].FullName));
                    }
                    GetList(dinfo[m].FullName + @"\", tn.Nodes[tn.Nodes.Count - 1], times - 1);
                }
            }
            catch { }
        }

        //鼠标单击树节点事件
        private void fileTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)  //单击鼠标左键才响应
            {
                try
                {
                    //在ListView中显示结点对应目录下的所有子目录、文件
                    string dir = e.Node.Name;
                    createItem(dir);
                    createHeader();
                }
                //例如访问被拒绝
                catch { }
            }
        }

        //为listview添加列名
        private void createHeader()   
        {
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "文件名";
            this.fileList.Columns.Add(ch);
            this.fileList.Columns[0].Width = 180;

            ch = new ColumnHeader();
            ch.Text = "大小(Byte)";
            this.fileList.Columns.Add(ch);
            this.fileList.Columns[1].Width = 100;

            ch = new ColumnHeader();
            ch.Text = "修改日期";
            this.fileList.Columns.Add(ch);
            this.fileList.Columns[2].Width = 175;

        }

        //设置ListView
        private void createItem(string root)
        {
            ListViewItem lvi;
            ListViewItem.ListViewSubItem lvsi;
            DirectoryInfo dir = new DirectoryInfo(root);
            DirectoryInfo[] dirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();

            fileList.Clear();

            fileList.BeginUpdate();
            foreach (DirectoryInfo di in dirs)  //把文件夹信息添加到listview的选项中
            {
                lvi = new ListViewItem();
                lvi.Text = di.Name;
                lvi.Tag = di.FullName;
                lvi.ImageIndex = 1;

                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = "";
                lvi.SubItems.Add(lvsi);

                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = di.LastAccessTime.ToString();
                lvi.SubItems.Add(lvsi);

                this.fileList.Items.Add(lvi);
            }

            foreach (FileInfo fi in files)  //把文件信息添加到listview的选项中
            {
                lvi = new ListViewItem();
                lvi.Text = fi.Name;
                lvi.ImageIndex = 0;
                lvi.Tag = fi.FullName;

                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = fi.Length.ToString();
                lvi.SubItems.Add(lvsi);

                lvsi = new ListViewItem.ListViewSubItem();
                lvsi.Text = fi.LastAccessTime.ToString();

                lvi.SubItems.Add(lvsi);

                this.fileList.Items.Add(lvi);

            }
            this.fileList.EndUpdate();
        }

        //鼠标双击ListView的item事件
        private void fileList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //获取点击的Item对应的路径
            ListViewHitTestInfo info = fileList.HitTest(e.X, e.Y);
            string path = ((ListViewItem)info.Item).Tag.ToString();   

            //打开路径下的文件，包括但不限于.txt及.exe文件
            ProcessStartInfo fileInfo = new ProcessStartInfo(path);
            fileInfo.Arguments = "";
            Process.Start(fileInfo);
        }

        //选择目录
        private string selectDir()
        {
            FolderBrowserDialog dir = new FolderBrowserDialog();
            if (dir.ShowDialog() == DialogResult.OK)
                return dir.SelectedPath;
            else
                return null;
        }

        //选择文件
        private string selectFilePath()
        {
            OpenFileDialog path = new OpenFileDialog();
            if (path.ShowDialog() == DialogResult.OK)
                return path.FileName;
            else
                return null;
        }

        //菜单、工具栏new dir选项
        private void newDirMenuItem_Click(object sender, EventArgs e)
        {
            string dir = selectDir();
            if (dir != null)
            {
                dir += "\\新建文件夹";
                int i = 1;
                string path = dir;
                //路径下已存在同名目录时，对目录名附加诸如(1)的形式，直至不再重名
                while (Directory.Exists(path))
                {
                    path = dir + " (" + i + ")";
                    i++;
                }
                //创建目录
                Directory.CreateDirectory(path);
            }
        }

        //菜单、工具栏new file选项
        private void newFileMenuItem_Click(object sender, EventArgs e)
        {
            string dir = selectDir();
            if (dir != null)
            {
                dir += "\\新建文本文档";
                int i = 1;
                string path = dir + ".txt";
                //路径下已存在同名文件时，对文件名附加诸如(1)的形式，直至不再重名
                while (File.Exists(path))
                {
                    path = dir + " (" + i + ").txt";
                    i++;
                }
                //创建文件
                File.Create(path);
            }
        }

        //菜单、工具栏open file选项
        private void openFileMenuItem_Click(object sender, EventArgs e)
        {
            string path = selectFilePath();
            if(path != null)
            {
                ProcessStartInfo fileInfo = new ProcessStartInfo(path);
                fileInfo.Arguments = "";
                Process.Start(fileInfo);
            }
        }
    }
}
